<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <title>汽运主单列表</title>
    <% include("/common/common_css_js.html"){} %>
    <script type="text/javascript" src="${base!}/js/LodopFuncs.js?d=${jdate!}"></script>
</head>
<body>
<object style="display: none" id="LODOP" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0>
    <embed id="LODOP_EM" type="application/x-print-lodop" width=0 height=0></embed>
</object>

<div style="padding-bottom: 5px;">
    <div id="queryForm" class="uc-toolbaroverflow" _borderStyle="border-top:0;">
        <span>主单号：</span>
        <input id="keyMasterBillCode" name="keyMasterBillCode" emptyText="请输入主单号" class="mini-textbox"
               selectOnFocus="true" required="false" requiredErrorText="主单号不能为空"/>
        <span class="separator"></span>
        <span>承运方式:</span>
        <input id="keyTransportWayId" name="keyTransportWayId" class="mini-combobox" style="width:150px;"
               textField="name" valueField="id" emptyText="请选择..."
               required="false" allowInput="false" showNullItem="true" nullItemText="请选择..."
               requiredErrorText="没有选择承运方式"/>
        <span class="separator"></span>
        <span>对账状态:</span>
        <input id="keyAccountCheckStatusId" name="keyAccountCheckStatusId" class="mini-combobox" style="width:120px;"
               textField="name" valueField="id" emptyText="请选择..."
               required="false" allowInput="false" showNullItem="true" nullItemText="请选择..."
               requiredErrorText="没有选择对账状态"/>
        <span class="separator"></span>
        <span>开单日期：</span>
        <input id="keyOpenMasterBillStartDate" name="keyOpenBillStartDate" class="mini-datepicker" format="yyyy-MM-dd"/>
        --
        <input id="keyOpenMasterBillEndDate" name="keyOpenBillEndDate" class="mini-datepicker" format="yyyy-MM-dd"/>

        <span>承运单位:</span>
        <div id="keyOutUnitId" name="keyOutUnitId"
             class="mini-combobox"
             style="width:250px;"
             popupWidth="600" textField="name" valueField="id"
             allowInput="true" required="false" requiredErrorText="外发单位不能为空"
             emptyText="承运单位名称">
            <div property="columns">
                <div header="单位名称" field="name"></div>
                <div header="联系人" field="contact"></div>
                <div header="电话" field="phone"></div>
                <div header="地址" field="address"></div>
            </div>
        </div>


        <div style="margin-top: 5px"></div>

        <% if(so.hasPermission("bMasterBillAuto:query")){%>
        <a class="mini-button" iconCls="icon-search" onclick="searchMasterBill()">查询</a>
        <% } %>

        <% if(so.hasPermission("bMasterBillAuto:queryCndClear")){%>
        <a class="mini-button" iconCls="icon-new" onclick="clearSearch()">清空查询条件</a>
        <% } %>

        <span class="separator"></span>

        <% if(so.hasPermission("bMasterBillAuto:edit")){%>
        <a class="mini-button" iconCls="icon-edit" onclick="editMasterBillShow()">编辑</a>
        <% } %>

        <% if(so.hasPermission("bMasterBillAuto:invalid")){%>
        <a class="mini-button" iconCls="icon-cancel" onclick="cancelMasterBill()">作废</a>
        <% } %>

        <% if(so.hasPermission("bMasterBillAuto:delete")){%>
        <a class="mini-button" iconCls="icon-remove" onclick="deleteMasterBill()">删除</a>
        <% } %>
        <% if(so.hasPermission("bMasterBillAuto:pull")){%>
        <a class="mini-button" iconCls="icon-edit" onclick="dropMasterBillShow()">拉货</a>
        <% } %>

        <span class="separator"></span>
        <a class="mini-button" iconCls="icon-edit" onclick="showPrintMasterBill()">主单打印</a>

        <span class="separator"></span>

        <% if(so.hasPermission("bMasterBillAuto:download")){%>
        <#download_grid id="downloadGrid" theGridId="datagrid1" theFileName="汽运主单信息表"/>
        <% } %>

        <div style="margin-top: 5px"></div>
        <span style="color: red;font-weight: bold">
                    <span>注:1.【初始配载分单号】表示主单初始配载时所包含的分单号 </span>
                    <span style="margin-left: 10px">2.主单如果做过全拉货操作，则主单下配载分单会被清空，而列表中显示的【初始配载分单号】不会清除(用于标识地面运费成本标识) </span>
                </span>
    </div>
</div>


<div class="mini-fit">
    <div id="datagrid1" class="mini-datagrid"
         style="width: 100%; height: 100%;"
         url="${base}/pages/master_bill_manage/auto/queryAutoMasterBill" idField="id"
         showPager="true" pageSize="20" sizeList="[10,20,30,50,100]"
         multiSelect="true" allowResize="true"
         showGroupsSummary="true" showSummaryRow="true" summaryPosition="bottom"
         allowSortColumn="true"
         sortField="createTime"
         sortOrder="desc"
         onshowrowdetail="onShowRowDetail"
         ondrawsummarycell="summaryRight"
    >
        <div property="columns">
            <div type="checkcolumn"></div>
            <div type="expandcolumn"></div>
            <div field="id" width="140" headerAlign="center" align="center" allowSort="true"
                 renderer="showMasterBillRender">汽运主单号
            </div>
            <div field="loadNativeBillIds" width="140" headerAlign="center" align="center">初始配载分单号</div>
            <div field="transportWayId" width="140" headerAlign="center" align="center" allowSort="true"
                 data-options='{cacheName:"sysDic"}' renderer="commonRender">承运方式
            </div>
            <div field="transportUnitId" width="140" headerAlign="center" align="left" allowSort="true"
                 data-options='{cacheName:"selfOrOutUnit"}' renderer="commonRender">承运单位
            </div>
            <div field="transportUnitMasterBillCodeIds" width="140" headerAlign="center" align="center"
                 allowSort="true">承运单位主单号IDS
            </div>
            <div field="startCityId" width="140" headerAlign="center" align="center" allowSort="true"
                 data-options='{cacheName:"city"}' renderer="commonRender">始发站
            </div>
            <div field="destCityId" width="140" headerAlign="center" align="center" allowSort="true"
                 data-options='{cacheName:"city"}' renderer="commonRender">目站市
            </div>

            <div field="autoLineRoadName" width="140" headerAlign="center" align="center" allowSort="true">线路名称</div>
            <div field="sendWayId" width="140" headerAlign="center" align="center" allowSort="true"
                 data-options='{cacheName:"sysDic"}' renderer="commonRender">派送方式
            </div>
            <!--                    <div field="pickUpPointId" width="140" headerAlign="center" align="center" allowSort="true">提货点ID</div>-->
            <div field="pickUpPointName" width="140" headerAlign="center" align="center" allowSort="true">提货点名称</div>
            <div field="pickUpPointPhone" width="140" headerAlign="center" align="center" allowSort="true">提货点电话</div>
            <div field="pickUpPointAddress" width="140" headerAlign="center" align="center" allowSort="true">提货点地址</div>

            <div field="licensePlateNumber" width="140" headerAlign="center" align="center" allowSort="true">车牌号</div>
            <div field="deliveryTime" width="140" headerAlign="center" align="center" allowSort="true"
                 dateFormat="yyyy-MM-dd HH:mm:ss" renderer="ondayRenderer">走货时间
            </div>
            <div field="driver" width="140" headerAlign="center" align="center" allowSort="true">驾驶员</div>
            <div field="driverPhone" width="140" headerAlign="center" align="center" allowSort="true">驾驶员电话</div>
            <div field="goodsTypeId" width="140" headerAlign="center" align="center" allowSort="true"
                 data-options='{cacheName:"sysDic"}' renderer="commonRender">货物类型
            </div>

            <div field="totalNumber" width="140" headerAlign="center" align="right" allowSort="false" summaryType="sum">
                总件数
            </div>
            <div field="actualWeight" width="140" headerAlign="center" align="right" allowSort="false"
                 renderer="public_moneyRenderer" summaryType="sum">实际重量
            </div>
            <div field="chargeableWeight" width="140" headerAlign="center" align="right" allowSort="false"
                 renderer="public_moneyRenderer" summaryType="sum">计费重量
            </div>
            <div field="weightRate" width="140" headerAlign="center" align="right" allowSort="false"
                 renderer="public_moneyRenderer">重量费率
            </div>
            <div field="volume" width="140" headerAlign="center" align="right" allowSort="false"
                 renderer="public_moneyRenderer" summaryType="sum">体积
            </div>
            <div field="volumeRate" width="140" headerAlign="center" align="right" allowSort="false"
                 renderer="public_moneyRenderer">体积费率
            </div>

            <div field="freightAmount" width="140" headerAlign="center" align="right" allowSort="false"
                 renderer="public_moneyRenderer" summaryType="sum">运费合计
            </div>
            <div field="otherCharges" width="140" headerAlign="center" align="right" allowSort="false"
                 renderer="public_moneyRenderer" summaryType="sum">其他费用
            </div>
            <div field="otherChargesComment" width="140" headerAlign="center" align="center" allowSort="true">其他费用说明
            </div>

            <div field="masterBillPaymentMethodId" width="140" headerAlign="center" align="center" allowSort="false"
                 data-options='{cacheName:"sysDic"}' renderer="commonRender">付款方式
            </div>
            <div field="totalFreightAmount" width="140" headerAlign="center" align="right" allowSort="false"
                 renderer="public_moneyRenderer" summaryType="sum">总运费
            </div>
            <div field="dropAmount" width="140" headerAlign="center" align="right" allowSort="false"
                 renderer="public_moneyRenderer" summaryType="sum">调整金额
            </div>
            <div field="settlementAmount" width="140" headerAlign="center" align="right" allowSort="false"
                 renderer="public_moneyRenderer" summaryType="sum">结算金额
            </div>

            <div field="openMasterBillDate" width="140" headerAlign="center" align="center" allowSort="false"
                 dateFormat="yyyy-MM-dd HH:mm:ss" renderer="ondayRenderer">开单日期
            </div>
            <div field="openMasterBillDepartmentId" width="140" headerAlign="center" align="center" allowSort="false"
                 data-options='{cacheName:"department"}' renderer="commonRender">开单部门
            </div>
            <div field="openMasterBillUserId" width="140" headerAlign="center" align="center" allowSort="false"
                 data-options='{cacheName:"sysUser"}' renderer="commonRender">开单人
            </div>
            <div field="comment" width="140" headerAlign="center" align="center" allowSort="false">备注</div>
            <div field="freightAmountStatusId" width="140" headerAlign="center" align="center" allowSort="false"
                 data-options='{cacheName:"sysDic"}' renderer="commonRender">运费结清状态
            </div>
            <div field="dropStatusId" width="140" headerAlign="center" align="center" allowSort="false"
                 data-options='{cacheName:"sysDic"}' renderer="commonRender">拉货状态
            </div>
            <div field="accountCheckStatusId" width="140" headerAlign="center" align="center" allowSort="false"
                 data-options='{cacheName:"sysDic"}' renderer="commonRender">对账状态
            </div>
            <div field="settlementStatusId" width="140" headerAlign="center" align="center" allowSort="false"
                 data-options='{cacheName:"sysDic"}' renderer="commonRender">结算状态
            </div>

            <div field="createTime" width="140" headerAlign="center" align="center" allowSort="false"
                 dateFormat="yyyy-MM-dd HH:mm:ss" renderer="ondayRenderer">创建时间
            </div>
            <div field="createUserId" width="140" headerAlign="center" align="center" allowSort="false"
                 data-options='{cacheName:"sysUser"}' renderer="commonRender">创建用户
            </div>
            <div field="updateTime" width="140" headerAlign="center" align="center" allowSort="false"
                 dateFormat="yyyy-MM-dd HH:mm:ss" renderer="ondayRenderer">更新时间
            </div>
            <div field="updateUserId" width="140" headerAlign="center" align="center" allowSort="false"
                 data-options='{cacheName:"sysUser"}' renderer="commonRender">更新用户
            </div>
        </div>
    </div>

</div>

<!-- 子表 -->
<div id="detailDiv" style="display:none; margin-left: 50px">
    <div id="detailGrid" class="mini-datagrid" style="width:1400px;height:200px;"
         pageSize="5" sizeList="[5,10,15,20]"
         url="${base}/pages/master_bill_manage/auto/queryNativeBillList">
        <div property="columns">
            <div field="nativeBillId" width="50" headerAlign="center" align="center" renderer="showNativeBillRender">
                分单号
            </div>
            <div field="loadNumber" width="70" headerAlign="center" align="right">配载总件数</div>
            <div field="loadActualWeight" width="70" headerAlign="center" align="right">配载实际重量</div>
            <div field="loadChargeableWeight" width="70" headerAlign="center" align="right">配载计费重量</div>
            <div field="loadVolume" width="70" headerAlign="center" align="right">配载体积</div>
            <div field="flightName" width="70" headerAlign="center" align="center">班次名称</div>
            <div field="flightDate" width="70" headerAlign="center" align="center" dateFormat="yyyy-MM-dd HH:mm:ss"
                 renderer="ondayRenderer">班次日期
            </div>
            <div field="shareTransportCostAmount" width="70" headerAlign="center" align="right" allowSort="false"
                 renderer="public_moneyRenderer">运费成本
            </div>
            <div field="shareGroundAmount" width="70" headerAlign="center" align="right" allowSort="false"
                 renderer="public_moneyRenderer">地面成本
            </div>
            <div field="shareAirlinesAddCostAmount" width="70" headerAlign="center" align="right" allowSort="false"
                 renderer="public_moneyRenderer">附加费成本
            </div>
            <div field="shareOtherChargesAmount" width="70" headerAlign="center" align="right" allowSort="false"
                 renderer="public_moneyRenderer">其他成本
            </div>
            <div field="shareTotalCostAmount" width="70" headerAlign="center" align="right" allowSort="false"
                 renderer="public_moneyRenderer">成本合计
            </div>
            <div field="shareTotalCostComment" width="140" headerAlign="center" align="center">成本说明</div>
        </div>
    </div>
</div>

<!-- 正单打印 -->
<div id="printMasterBillPanel" class="mini-window" title="主单打印" style="width:1200px;height:800px;"
     showMaxButton="true" showCollapseButton="false" showMinButton="true" showShadow="true"
     showToolbar="true" showFooter="true" showModal="true" allowResize="true" allowDrag="true">

    <div property="footer" style="padding:10px;text-align: right">
        选择打印机:
        <select id="printList">
            <option></option>
        </select>

        <a class="mini-button" iconCls="icon-ok" onclick="printMasterBill('1')">打印</a>
        <a class="mini-button" iconCls="icon-ok" onclick="printMasterBill('2')">打印预览</a>
        <a class="mini-button" iconCls="icon-ok" onclick="printMasterBill('3')">页面设置</a>

        <a class="mini-button" iconCls="icon-close" onclick="public_onCancel('printMasterBillPanel')">关闭</a>
    </div>


    <!--需要打印的区域-->
    <div id="printArea" style="width: 98%">

    </div>
</div>


<script type="text/javascript">
    mini.parse();

    let keyMasterBillCode = mini.get("keyMasterBillCode");
    let keyTransportWayId = mini.get("keyTransportWayId");
    let keyOpenMasterBillStartDate = mini.get("keyOpenMasterBillStartDate");
    let keyOpenMasterBillEndDate = mini.get("keyOpenMasterBillEndDate");
    let keyAccountCheckStatusId = mini.get("keyAccountCheckStatusId");

    let grid = mini.get("datagrid1");
    let detailDiv = document.getElementById("detailDiv");
    let detailGrid = mini.get("detailGrid");

    let autoPullGoodsDatagrid = mini.get("autoPullGoodsDatagrid");


    // 拉货主单号
    let masterBillId = mini.get("masterBillId");

    // 已经选择的主单对象
    let selectMasterItem;

    $(function () {
        // 承运方式下拉列表
        let keyTransportWayIdJsonData = commonRenderSysDicDropListByKey("sysDic", "XT_DIC_TRANSPORT_WAY");
        keyTransportWayId.load(keyTransportWayIdJsonData);

        // 对账状态
        let keyAccountCheckStatusIdJsonDate = commonRenderSysDicDropListByKey("sysDic", "XT_DIC_ACCOUNT_CHECK_STATUS");
        keyAccountCheckStatusId.load(keyAccountCheckStatusIdJsonDate);


        // 加载外发单位下拉列表
        let outUnitIdJsonData = commonRenderDropList("outUnit");
        mini.get("keyOutUnitId").load(outUnitIdJsonData);

        // 加载列表数据
        grid.load();
    });

    function clearSearch() {
        let form = new mini.Form("#queryForm");
        form.clear();
    }


    function showMasterBillRender(e) {
        // renderer="showMasterBillRender"

        let masterBillId = e.value;
        let htmlStr = "<a href='javascript:void(0);' onclick='showViewMasterBill(\"" + masterBillId + "\")'>" + masterBillId + "</a>";

        return htmlStr;
    }

    function showViewMasterBill(masterBillId) {

        let tabUrl = "${base}/pages/master_bill_manage/auto/showUpdateMasterBill?opType=view&id=" + masterBillId;
        let tabTitle = "汽运主单查看";

        parent.window.addTabToMainPage(tabTitle, tabTitle, tabUrl);
    }


    function showNativeBillRender(e) {
        //  renderer="showNativeBillRender"
        let nativeBillId = e.value;
        let htmlStr = "<a href='javascript:void(0);' onclick='showViewNativeBill(\"" + nativeBillId + "\")'>" + nativeBillId + "</a>";
        return htmlStr;
    }

    function showViewNativeBill(nativeBillId) {
        let tabUrl = "${base}/pages/native_bill_manage/showUpdateBill?opType=view&id=" + nativeBillId;
        parent.window.addTabToMainPage("分单查看", "分单查看", tabUrl);
    }

    /**
     * 根据查询条件查询主单
     */
    function searchMasterBill() {
        let form = new mini.Form("#queryForm");
        let data = form.getData(true);
        grid.load(data);
    }

    /**
     * 加载明细
     */
    function onShowRowDetail(e) {
        let grid = e.sender;
        let row = e.record;

        let td = grid.getRowDetailCellEl(row);
        td.appendChild(detailDiv);

        detailDiv.style.display = "block";

        detailGrid.load({masterBillId: row.id});

    }

    /**
     * 主单编辑
     */
    function editMasterBillShow() {
        let masterBillObjs = grid.getSelecteds();
        if (masterBillObjs.length > 0) {
            if (masterBillObjs.length == 1) {
                let tabUrl = "${base}/pages/master_bill_manage/auto/showUpdateMasterBill?opType=update&id=" + masterBillObjs[0].id;
                parent.window.addTabToMainPage("汽运主单编辑", "汽运主单编辑", tabUrl);
            } else {
                showAlert("编辑时请选择一行！");
            }
        } else {
            showAlert("请选择需要编辑的对象！");
        }
    }

    /**
     * 主单作废
     */
    function cancelMasterBill() {
        showAlert("暂不提供主单作废功能!");
    }

    let settlementStatus = {uncleared: 321, cleared: 322};

    let accountCheckStatus = {noCheck: 431, checked: 432};

    /**
     * 主单删除
     */
    function deleteMasterBill() {
        let masterBillObjs = grid.getSelecteds();
        if (masterBillObjs.length == 0) {
            showAlert("请选择需要删除的主单!");
            return;
        }

        // 可以删除的主单
        /**
         * 主单删除
         1.主单删除时如果主单的对账状态是已对账，则不能删除
         2.主单删除时其下的相关配载信息会一并删除，拉货信息也会一并删除
         3.如果是自发的主单号会被回收，供再次使用
         3.分单不会删除，但会修改其配载状态为未配载或部分配载，原分单成本及分单利润会调整为0  drop_status_id
         */
        let canDeleteMasterBillIds = "";
        // 不可以删除的主单
        let canNotDeleteMasterBillIds = "";

        console.log(masterBillObjs);

        for (let obj of masterBillObjs) {
            if (obj.settlementStatusId == settlementStatus.cleared
                || obj.accountCheckStatusId == accountCheckStatus.checked
                || obj.dropStatusId == pullStatus.pull) {
                canNotDeleteMasterBillIds += obj.id + ",";
            } else {
                canDeleteMasterBillIds += obj.id + ",";
            }
        }

        if (canNotDeleteMasterBillIds.length > 0) {
            canNotDeleteMasterBillIds = canNotDeleteMasterBillIds.substring(0, canNotDeleteMasterBillIds.length - 1);
            showAlert("主单【" + canNotDeleteMasterBillIds + "】 为【已拉货、已对账、已结算】，不能删除!");
            return;
        }

        if (canDeleteMasterBillIds.length == 0) {
            return;
        }

        canDeleteMasterBillIds = canDeleteMasterBillIds.substring(0, canDeleteMasterBillIds.length - 1);

        // 弹出删除确认对话框
        mini.confirm("确定删除已经选择【" + canDeleteMasterBillIds + "】主单？", "主单删除",
            function (action) {
                if (action == "ok") {
                    // String statusFieldName, Long statusId, String billIds,  cacel_status_id
                    let ret = public_getServerData("${base}/pages/master_bill_manage/auto/deleteMasterBillByMasterBillIds", {masterBillIds: canDeleteMasterBillIds});
                    showAlert(ret.message, function () {
                        if (ret.ok) {
                            grid.reload();
                        }
                    });
                }
            }
        );
    }


    // 拉货状态
    let pullStatus = {notPull: 361, pull: 362};

    // 主单类型
    let masterBillType = {flight: 261, auto: 262};

    function dropMasterBillShow() {

        let masterBillObjs = grid.getSelecteds();

        if (masterBillObjs.length == 0) {
            showAlert("请选择需要拉货的主单！");
            return;
        }

        if (masterBillObjs.length > 1) {
            showAlert("拉货时请选择单行！");
            return;
        }

        let masterBillId = masterBillObjs[0].id;

        // 这个地方应该从数据库加载，因为可能做过了现在双重复做,根据 ID 再加载一下对象
        let ret = public_getServerData("${base}/pages/master_bill_manage/auto/queryMasterBillAutoPojoById?masterBillId=" + masterBillId);
        if (!ret.ok) {
            showAlert("主单拉货主单对象加载失败!");
            return;
        }


        if (ret.data.dropStatusId == pullStatus.pull) {
            showAlert("主单【" + masterBillObjs[0].id + "】已经做过拉货，不能重复拉货!");
            return;
        }


        let tabUrl = "${base}/pages/master_bill_manage/pull/showPullAdd?masterBillId=" + masterBillId + "&masterBillTypeId=" + masterBillType.auto;
        parent.window.addTabToMainPage("主单拉货", "主单拉货", tabUrl);

    }


    /**
     * 设置底部统计样式
     * @param e
     */
    function summaryRight(e) {
        e.cellStyle = "text-align:right;color:red;font-size:18px;font-weight:bold";
    }

    /**
     * 正单打印
     */

    let displayHtml;
    // 发货人付款方式类型
    let deliveryCustomerPaymentMethodType = {
        cash: 151,
        alipay: 152,
        weChat: 153,
        unionpay: 154,
        arrears: 156,
        noMoneyReceive: 157,
        toPay: 158
    };

    function showPrintMasterBill() {
        let masterBillObjs = grid.getSelecteds();
        if (masterBillObjs.length > 0) {
            if (masterBillObjs.length == 1) {
                // 显示拉货的列表信息  airPullGoodsPanel
                selectMasterItem = masterBillObjs[0];
                // 根据主单ID 获取主单配载的分单对象
                let ret = public_getServerData("${base}/pages/master_bill_manage/auto/queryBNativeBillPojoListByMasterBillId?masterBillId=" + selectMasterItem.id);
                if (!ret.ok) {
                    showAlert("主单下不包含配载分单，可能是由于拉货导致的!");
                    return;
                }

                let billArray = ret.data;
                if (billArray.length == 0) {
                    showAlert("根据主单号获取配载分单信息失败，请查看主单下是否包含配载分单信息!");
                    return;
                }

                public_showModelAtPos("printMasterBillPanel", "center", "middle");


                let transportUnitName = commonRendderNameByKeyAndId("selfOrOutUnit", selectMasterItem.transportUnitId); // 承运单位

                let row1Html = "<div style='font-weight:bold;font-size: 20px'>To:" + transportUnitName + "</div>"

                // 第二行直接输出
                let transportUnitId = selectMasterItem.transportUnitId;
                let outUnitObj = commonRendderObjByKeyAndId("outUnit",transportUnitId);
                let masterBillForeignCompanyObj = commonRendderObjByKeyAndId("sysCompany",outUnitObj.masterBillForeignCompanyId);
                let masterBillForeignCompanyName = masterBillForeignCompanyObj.name;
                let row2Html = "<div style='width: 100%;text-align: center;font-weight: bold;font-size: 20px'>" + masterBillForeignCompanyName + "出港货物清单</div>";

                // 第三行
                let row3Html = "<div style='width: 100%;font-size: 15px;margin-top: 10px'>"
                row3Html += "<span style='width: 20%;display:inline-block;'>提货单号:" + selectMasterItem.id + "</span>";
                row3Html += "<span style='width: 20%;display:inline-block;'>班次日期:" + public_cstDataConvert(selectMasterItem.deliveryTime) + "</span>";
                row3Html += "<span style='width: 20%;display:inline-block;'>车牌号:" + selectMasterItem.licensePlateNumber + "</span>";
                row3Html += "<span style='width: 20%;display:inline-block;'>驾驶员:" + selectMasterItem.driver + "</span>";
                row3Html += "<span style='width: 20%;display:inline-block;'>电话:" + selectMasterItem.driverPhone + "</span>";
                row3Html += "</div>";

                // 第四行，分单显示区域

                let totalAgentDeliveryCharge = 0;
                let totalDeliveryToPayAmount = 0;
                let totalCollectionMoney = 0;

                let tableHtml = "<table width='100%' border='1' cellspacing='0' cellpadding='0'>";
                tableHtml += "<tr>";
                tableHtml += "<th style='text-align: center'>分单号</th>"
                tableHtml += "<th style='text-align: center'>件数</th>"
                tableHtml += "<th style='text-align: center'>实重</th>"
                tableHtml += "<th style='text-align: center'>体积</th>"
                tableHtml += "<th style='text-align: center'>目的站</th>"
                tableHtml += "<th style='text-align: center'>品名/包装</th>"
                tableHtml += "<th style='text-align: center'>收货人</th>"
                tableHtml += "<th style='text-align: center'>提货方式</th>"
                tableHtml += "<th style='text-align: center'>签收方式</th>"
                tableHtml += "<th style='text-align: center'>派送费</th>"
                tableHtml += "<th style='text-align: center'>收货人付款方式</th>"
                tableHtml += "<th style='text-align: center'>收货人付款金额</th>"
                tableHtml += "<th style='text-align: center'>代收货款</th>"
                tableHtml += "<th style='text-align: center'>备注</th>"
                tableHtml += "</tr>";

                // 通过循环输出分单位对象并累计相应数据
                // let deliveryCustomerPaymentMethodType = {cash:151,alipay:152,weChat:153,unionpay:154,arrears:156,noMoneyReceive:157,toPay:158};
                for (let billObj of billArray) {
                    totalAgentDeliveryCharge += public_toNumber(billObj.agentDeliveryCharge, 0);
                    if (billObj.deliveryCustomerPaymentMethodId == deliveryCustomerPaymentMethodType.toPay) {
                        totalDeliveryToPayAmount += public_toNumber(billObj.deliveryCustomerPaymentAmount, 0);
                    }
                    totalCollectionMoney += public_toNumber(billObj.collectionMoney, 0);

                    let destCityName = commonRendderNameByKeyAndId("city", billObj.destCityId); // 目的站
                    let packingWayName = commonRendderNameByKeyAndId("sysDic", billObj.packingWayId); // 包装
                    let pickUpGoodsWayName = commonRendderNameByKeyAndId("sysDic", billObj.pickUpGoodsWayId); // 提货方式
                    let receiveCustomerPaymentMethodName = "";
                    let receiveCustomerPaymentAmount = "";
                    if (!public_isEmpty(billObj.receiveCustomerPaymentMethodId) && billObj.receiveCustomerPaymentMethodId == 165) {
                        // 注意165代表收货人付款方式为到付的
                        receiveCustomerPaymentMethodName = commonRendderNameByKeyAndId("sysDic", billObj.receiveCustomerPaymentMethodId);  // 收货人付款方式
                        if (billObj.receiveCustomerPaymentAmount > 0) {
                            receiveCustomerPaymentAmount = billObj.receiveCustomerPaymentAmount;
                        }
                    }
                    let signChooseName = "";
                    if (!public_isEmpty(billObj.signChooseId)) {
                        signChooseName = commonRendderNameByKeyAndId("sysDic", billObj.signChooseId); // 签收方式
                    }
                    tableHtml += "<tr>";

                    tableHtml += "<td style='text-align: center'>" + billObj.id + "</td>"; // 分单号
                    tableHtml += "<td style='text-align: center'>" + billObj.totalNumber + "</td>";  // 件数
                    tableHtml += "<td style='text-align: center'>" + billObj.actualWeight + "</td>"; // 实重
                    tableHtml += "<td style='text-align: center'>" + billObj.volume + "</td>"; // 体积
                    tableHtml += "<td style='text-align: center'>" + destCityName + "</td>"; //  目的站
                    tableHtml += "<td style='text-align: center'>" + billObj.goodsName + "/" + packingWayName + "</td>";  // 品名/包装
                    tableHtml += "<td style='padding-left: 5px'>" + billObj.receiveCustomerName + "<br/>" + billObj.receiveCustomerPhone + "<br/>" + billObj.receiveCustomerAddress + "</td>"; // 收货人
                    tableHtml += "<td style='text-align: center'>" + pickUpGoodsWayName + "</td>"; // 提货方式
                    tableHtml += "<td style='text-align: center'>" + signChooseName + "</td>"; // 签收方式
                    tableHtml += "<td style='text-align: center'>" + billObj.agentDeliveryCharge + "</td>"; // 代理派送费
                    // tableHtml += "<td style='text-align: center'>" + deliveryCustomerPaymentMethodName + " / " + billObj.deliveryCustomerPaymentAmount + "</td>"; // 发货人付款方式
                    tableHtml += "<td style='text-align: center'>" + receiveCustomerPaymentMethodName + "</td>"; // 收货人付款方式
                    tableHtml += "<td style='text-align: center'>" + receiveCustomerPaymentAmount + "</td>"; // 收货人付款金额
                    tableHtml += "<td style='text-align: center'>" + billObj.collectionMoney + "</td>"; //  代收货款
                    tableHtml += "<td style='padding-left: 5px'>" + billObj.comment + "</td>"; //  备注

                    tableHtml += "</tr>";

                }

                tableHtml += "</table>"

                let row4Html = "<div style='width: 100%;font-size: 15px'>" + tableHtml + "</div>";

                // 第五行
                let row5Html = "<div  style='width: 100%;font-size: 15px'>";
                row5Html += "<span style='width:15%; display:inline-block;'>主单总件数:" + selectMasterItem.totalNumber + "</span>";
                row5Html += "<span style='width:15%; display:inline-block;'>主单计重:" + selectMasterItem.chargeableWeight + "</span>";
                row5Html += "<span style='width:15%; display:inline-block;'>派送费合计:" + public_round2(totalAgentDeliveryCharge) + "</span>";
                row5Html += "<span style='width:15%; display:inline-block;'>发货人到付合计:" + public_round2(totalDeliveryToPayAmount) + "</span>";
                row5Html += "<span style='width:15%; display:inline-block;'>代收款合计:" + public_round2(totalCollectionMoney) + "</span>";
                row5Html += "</div>";


                // 第六行
                let masterBillComment = selectMasterItem.comment;
                if (public_isEmpty(masterBillComment) || masterBillComment == "null") {
                    masterBillComment = "";
                }
                let row6Html = "<div style='width: 100%;font-size: 15px'>主单备注:" + masterBillComment + "</div>";
                // 等待打印与显示的HTML
                displayHtml = row1Html + row2Html + row3Html + row4Html + row5Html + row6Html;

                $("#printArea").empty();
                $("#printArea").append(displayHtml);

                getPrinterlist("printList");

            } else {
                showAlert("打印请选择单行！");
            }
        } else {
            showAlert("请选择需要打印的主单！");
        }
    }

    /**
     * 根据ID加载打印机列表
     */
    function getPrinterlist(printerId) {
        let printList = $("#" + printerId); //打印机下拉列表
        // 清空下拉列表
        printList.empty();
        printList.prepend("<option value='-1'>请选择</option>");

        let lodop = getLodop($("#LODOP_OB"), $("#LODOP_EM"));

        let printerAccount = lodop.GET_PRINTER_COUNT();

        for (let i = 0; i < printerAccount; i++) {
            let printerIndex = i.toString();
            let printerName = lodop.GET_PRINTER_NAME(i);
            printList.append("<option value='" + printerIndex + "'>" + printerName + "</option>");
        }
    }

    function printMasterBill(opType) {

        let selectedPrinterName = $("#printList").find("option:selected").text();
        ;
        if (public_isEmpty(selectedPrinterName) || selectedPrinterName == "请选择") {
            showAlert("请选择指定打印机!");
            return;
        }

        let LODOP = getLodop($("#LODOP"), $("#LODOP_EM"));
        //lodop.PRINT_INIT("打印详细信息");
        //lodop.PRINT_INITA(-107, -90, 800, 600, "套打EMS的模板");
        //lodop.SET_PRINT_PAGESIZE(1, "90mm", "55mm", ""); //设置纸张高度

        LODOP.PRINT_INIT("正单位打印");
        LODOP.SET_PRINT_PAGESIZE(2, 0, 0, "A4"); // A4 横向

        // ADD_PRINT_HTM(Top,Left,Width,Height,strHtmlContent)
        LODOP.ADD_PRINT_HTM("10mm", "5mm", "98%", "80%", displayHtml);

        if (opType == "1") {
            LODOP.SET_PRINTER_INDEXA(selectedPrinterName)
            LODOP.PRINT(); //直接打印

        } else if (opType == "2") {
            LODOP.SET_PRINTER_INDEXA(selectedPrinterName)
            LODOP.PREVIEW(); //打印预览
        } else {
            LODOP.PRINT_SETUP(); //打印维护
        }
    }


</script>

</body>
</html>